module(...,package.seeall)

function updateHeart(group, newLife)
    local heart =  "images/digits/heart.png"

    group:remove(1)

    local lifeGroup = display.newGroup()
    group:insert( lifeGroup )
	
	local perdeu = false;
	if newLife == 0 then
		perdeu = true
	end
	if not perdeu then
		for i=1, newLife do
			local hTemp = display.newImage(heart)
			hTemp.x = _W - (45 * i) + 10; hTemp.y = 60;
			lifeGroup:insert(hTemp)
		end
	end
	return perdeu;
end


function initHeart(group)
   
    local heart =  "images/digits/heart.png"

    local lifeGroup = display.newGroup()
    group:insert( lifeGroup )
	
	for i=1, 3 do
		local hTemp = display.newImage(heart)
		hTemp.x = _W - (45 * i) + 10; hTemp.y = 60;
		lifeGroup:insert(hTemp)
	end
end

function updateLife(currentLife, action)
    local newLife = 0

	if (action == "perdeu") then
		if currentLife > 0 then 
			newLife = currentLife - 1;
		end	
	else 
		if currentLife < 3 then 
			newLife = currentLife + 1;
		end
	end
	
	return newLife
end

function loseLife()
	_G.currentLife = updateLife(_G.currentLife, "perdeu")
	updateHeart(_G.lifeGroup, _G.currentLife)
end
